Strict

Import agfx

Global myTmpScreen:TTmpScreen

Class TAgfx_test_app Extends TFramework

	Method OnCreate:Int()
		
		Super.OnCreate()
				
		'DISPLAY.SetVirtualSize(480,320)
		
		myTmpScreen = New TTmpScreen		
		myTmpScreen.Prepare()
		
		Return 0
	End
	

	
End


Class TTmpScreen Extends TScreen
	
Const BOX_COEF:Float = 0.4
Const BOX_MASS:Float = 30


	
	Field obj:TVerletGroup
	Field p1:TVerletPoint
	Field p2:TVerletPoint
	Field p3:TVerletPoint
	Field p4:TVerletPoint

	Field pr:TVerletPoint[]
	Field pb:TVerletPoint[]
	
	Field rope_h:TVerletGroup
	Field bridge:TVerletGroup

	Field gravity:TVector2 = New TVector2(0,5)

	Field box_2:TVerletGroup
	
	' START when app is created
	Method Start:Void()
	
		Self.obj = TVerletGroup.Create( Self.gravity, False)
		
		Self.p1 = TVerletPoint.Create( 0,0,	  BOX_MASS, True)
		Self.p2 = TVerletPoint.Create( 64,0,  BOX_MASS, True)
		Self.p3 = TVerletPoint.Create( 0,64,  BOX_MASS, True)
		Self.p4 = TVerletPoint.Create( 64,64, BOX_MASS, True)
		
		Self.obj.AddPoint( p1)
		Self.obj.AddPoint( p2)
		Self.obj.AddPoint( p3)
		Self.obj.AddPoint( p4)
		
		Self.obj.AddLink( TVerletLink.Create( p1, p2, BOX_COEF)) ' top
		Self.obj.AddLink( TVerletLink.Create( p2, p4, BOX_COEF)) ' right
		Self.obj.AddLink( TVerletLink.Create( p4, p3, BOX_COEF)) ' bottom
		Self.obj.AddLink( TVerletLink.Create( p3, p1, BOX_COEF)) ' left
		Self.obj.AddLink( TVerletLink.Create( p3, p2, BOX_COEF)) ' cross 1
		Self.obj.AddLink( TVerletLink.Create( p1, p4, BOX_COEF)) ' cross 2
		
		
		' rotate it and give it some speed
		Self.obj.Rotate( 10)
		Self.obj.Translate( 300,64,True)
	
		' create rope A'
		Self.rope_h = TVerletGroup.Create(Self.gravity,False)
		pr = New TVerletPoint[10]
		For Local n:Int=0 to 9
			pr[n]    = TVerletPoint.Create(310,50+n*16,BOX_MASS,True)			
			Self.rope_h.AddPoint(pr[n])			
		Next
		
		For Local n:=0 To 8
			Self.rope_h.AddLink(TVerletLink.Create(pr[n],pr[n+1],BOX_COEF))			
		Next
		
		pr[0].active = False
		
		Self.rope_h.Rotate( 1)
	
		' create bridge
		Self.bridge = TVerletGroup.Create(Self.gravity,True)

		pb = New TVerletPoint[10]
		For Local n:Int=0 to 9
			pb[n]    = TVerletPoint.Create(150+n*32,DISPLAY.VSCREEN_HALF_HEIGHT,BOX_MASS,True)			
			Self.bridge.AddPoint(pb[n])			
		Next
		
		For Local n:=0 To 8
			Self.bridge.AddLink(TVerletLink.Create(pb[n],pb[n+1],BOX_COEF))			
		Next
		
		pb[0].active = False
		pb[9].active = False
		
		Self.box_2 = New TVerletGroup(Self.gravity,True)
		Self.box_2.CreateRectangle(300,256,128,32,20,0.2)
		
		obj.active = True
		Self.rope_h.active = True
		
		
	End
	
	' main app logic loop update
	Method Update:Void()

		
		Self.obj.Update()
		Self.rope_h.Update()
		Self.bridge.Update()
		Self.box_2.Update()
		
		If KeyHit( KEY_SPACE) Then 
			obj.active = Not obj.active
			Self.rope_h.active = Not Self.rope_h.active
		End if
		
		If KeyHit (KEY_UP) Then Self.obj.Translate( 0,-4)
		
		If KeyHit (KEY_LEFT) Then Self.obj.Rotate( -2)
		If KeyHit (KEY_RIGHT) Then Self.obj.Rotate( 2)
		
		' constrain all points to screen edges
		' BOX
		For Local p:TVerletPoint = EachIn obj.points
			' bottom
			If p.pos.y < 0 Then
				p.pos.y = 0
			End If
			If p.pos.y > DISPLAY.VSCREEN_HEIGHT Then 
				p.pos.y = DISPLAY.VSCREEN_HEIGHT
				' full friction
				p.old_pos.x = p.pos.x
			EndIf
			' left, right
			If p.pos.x < 0 Then
				p.pos.x = 0
			ElseIf p.pos.x > DISPLAY.VSCREEN_WIDTH Then
				p.pos.x = DISPLAY.VSCREEN_WIDTH
			EndIf
		
	Next

	' ROPE
	If MOUSE.Down() Then
			Local p:TVerletPoint = Self.rope_h.points.GetLast() 
			p.pos.x = MOUSE.x
			p.pos.y = MOUSE.y
			'pr[5].pos.x=
			'pr[5].pos.y=MOUSE.y
		End
		
		
	'BOX_2
		' constrain all points to screen edges
	For Local p:TVerletPoint = EachIn Self.box_2.points
		' bottom
		If p.pos.y < 0 Then
			p.pos.y = 0
		End If
		If p.pos.y > DISPLAY.VSCREEN_HEIGHT Then 
			p.pos.y = DISPLAY.VSCREEN_HEIGHT
			' full friction
			p.old_pos.x = p.pos.x
		EndIf
		' left, right
		If p.pos.x < 0 Then
			p.pos.x = 0
		ElseIf p.pos.x > DISPLAY.VSCREEN_WIDTH Then
			p.pos.x = DISPLAY.VSCREEN_WIDTH
		EndIf
		
		
	Next

	End
	
	' main app render loop
	Method Render:Void()
		Cls
		
		Self.obj.Render()
		Self.rope_h.Render()
		Self.bridge.Render()
		Self.box_2.Render()

		
		DrawText("LEFT/RIGHT   = rotate",10,20)
		DrawText("UP           = translate up",10,40)
		DrawText("SPACE        = start/stop",10,60)
	End
End
